//
//  UserDefaults.m
//  HuateVideo
//
//  Created by Tiger on 12-5-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UserDefaults.h"

@implementation UserDefaults

+ (void)setObject:(id)object forKey:(id)key
{
    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
    [[NSUserDefaults standardUserDefaults] setObject:encodedObject forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (id)objectForKey:(id)key
{
    NSData *encodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    return [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
}

+ (BOOL)restoreLocalVideo:(LocalVideo *)localVideo
{
    NSMutableArray *temp = [NSMutableArray array];
    if ([[UserDefaults objectForKey:MY_VIDEOS_STORE_KEY] count]) {
        [temp addObjectsFromArray:[UserDefaults objectForKey:MY_VIDEOS_STORE_KEY]];
    }
    
    for (int i=0; i<[temp count]; i++) {
        LocalVideo *lc = [temp objectAtIndex:i];
        if ([lc.fileid isEqualToString:localVideo.fileid]) {
            [temp replaceObjectAtIndex:i withObject:localVideo];
            [UserDefaults setObject:temp forKey:MY_VIDEOS_STORE_KEY];
            return YES;
        }
    }
    
    return NO;
}

+ (BOOL)deleteLocalVideo:(LocalVideo *)localVideo
{
    NSMutableArray *temp = [NSMutableArray array];
    if ([[UserDefaults objectForKey:MY_VIDEOS_STORE_KEY] count]) {
        [temp addObjectsFromArray:[UserDefaults objectForKey:MY_VIDEOS_STORE_KEY]];
    }
    
    for (int i=0; i<[temp count]; i++) {
        LocalVideo *lc = [temp objectAtIndex:i];
        if ([lc.fileid isEqualToString:localVideo.fileid]) {
            [temp removeObject:lc];
            [UserDefaults setObject:temp forKey:MY_VIDEOS_STORE_KEY];
            return YES;
        }
    }
    
    return NO;
}

@end
